﻿/*$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/config/list',
        datatype: "json",
        colModel: [			
			{ label: 'ID', name: 'id', width: 30, key: true },
			{ label: '商品名称', name: 'key', width: 60 },
			{ label: '货号', name: 'value', width: 30 },
			{ label: '价格', name: 'remark', width: 80 },
			{ label: '上架', name: 'state', width:10},
			{ label: '精品', name: 'hot', width:10},
			{ label: '新品', name: 'new', width:10},						
			{ label: '热销', name: 'state', width:10},	
			{ label: '推荐排序', name: 'state', width:20},
			{ label: '库存', name: 'state', width:10},
			{ label: '虚拟销量', name: 'state', width:20},
			{ label: '操作', name: 'state', width:25}				
						
        ],
				viewrecords: true,
        height: 385,
        rowNum: 10,
				rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
*/

//CKEDITOR.replace("detailarea",{toolbar:'Full',skin:'kama'})

Vue.use(VueI18n)

var initial = 'zh_cn'
var i18n = new VueI18n({
	locale: initial,
	messages: info
})

var vm = new Vue({
	el:'#tabdiv',
	i18n: i18n,
	data:{
		locale : initial,
		
		cb_alone_sale_1: true,
		cb_alone_sale:false,
		cb_free_shipping:false,
		cb_best:true,
		cb_new:false,
		cb_hot:true,
		
		if_code:true,
		if_cfg_storage:true,
		help_open:true,
		
		is_add:true,
		
		suppliers_exists: 1,
		
		marketPriceRate : 1,
  	integralPercent : 0,
		
		user_rank_list  : true,
		
		fonts: i18n.t("lang.font_styles"),
		goods: {},
		
		ue: '',
		uedata: [],
		savedata: [],
		
		q:{
			key: null
		},
		showList: true,
		title: null,
		config: {}
	},
	mounted() {
		this.ue = UE.getEditor('editor',{
			BaseUrl: '',
			UEDITOR_HOME_URL:'../../plugins/ueditor/',
		});
	},
	watch: {
    locale: function (val) {
      this.$i18n.locale = val
    }
  },
	methods: {
		submits: function(){
			this.uedata.push(UE.getEditor('editor').getContent());
			//console.log("this.uedata.join("\n"));
		},
		pushin: function(){
			UE.getEditor('editor').setContent('欢迎使用ueditor');
		}, 
		notice: function(msg,event){
			var obj = document.getElementById(msg);

		  if (obj)
		  {
		    if (obj.style.display != "block")
		    {
		      obj.style.display = "block";
		    }
		    else
		    {
		      obj.style.display = "none";
		    }
		  }
		},		
		hideCatDiv: function(){
			var category_add_div = document.getElementById("category_add");
      if(category_add_div.style.display != null)
      {
          category_add_div.style.display = 'none';
      }
		},
		rapidCatAdd: function(){
		 var cat_div = document.getElementById("category_add");

      if(cat_div.style.display != '')
      {
          var cat =document.getElementsByName('addedCategoryName');
          cat.value = '';
          cat_div.style.display = '';
      }
    }, 
    addCategory: function(){
      var parent_id = document.getElementsByName('cat_id');
      var cat = document.getElementsByName('addedCategoryName');
      if(cat.value.replace(/^\s+|\s+$/g, '') == '')
      {
          alert(category_cat_not_null);
          return;
      }

      var params = 'parent_id=' + parent_id.value;
      params += '&cat=' + cat.value;
      //Ajax.call('category.php?is_ajax=1&act=add_category', params, addCatResponse, 'GET', 'JSON');
  	}, 
  	goCatPage: function(){
  	  if(confirm(go_category_page))
      {
          window.location.href='category.php?act=add';
      }
      else
      {
          return;
      }
    },
    addOtherCat: function(conObj,event){
    	var sel = document.createElement("SELECT");
      var selCat = document.getElementsByName('cat_id');

      for (i = 0; i < selCat.length; i++)
      {
          var opt = document.createElement("OPTION");
          selCat = selCat[0];
          opt.text = selCat.options[i].text;
          opt.value = selCat.options[i].value;
          //if (Browser.isIE)
          //{
          //    sel.add(opt);
          //}
          //else
          //{
              sel.appendChild(opt);
          //}
      }
      event.currentTarget.parentNode.appendChild(sel);
      sel.name = "other_cat[]";
      sel.onChange = function() {checkIsLeaf(this);};
    },
    hideBrandDiv: function(){
     var brand_add_div = document.getElementById("brand_add");
      if(brand_add_div.style.display != 'none')
      {
          brand_add_div.style.display = 'none';
      }
    },  
    rapidBrandAdd: function(){
     var brand_div = document.getElementById("brand_add");

      if(brand_div.style.display != '')
      {
          var brand =document.getElementsByName('addedBrandName');
          brand.value = '';
          brand_div.style.display = '';
      }
    },  
    addBrand: function(){
     var brand = document.getElementsByName('addedBrandName');
      if(brand.value.replace(/^\s+|\s+$/g, '') == '')
      {
          alert(brand_cat_not_null);
          return;
      }

      var params = 'brand=' + brand.value;
      //Ajax.call('brand.php?is_ajax=1&act=add_brand', params, addBrandResponse, 'GET', 'JSON');
    },
    goBrandPage: function(){
    	if(confirm(go_brand_page))
      {
          window.location.href='brand.php?act=add';
      }
      else
      {
          return;
      }
    },
    priceSetted: function(){
    	this.computePrice('market_price', this.marketPriceRate);
    	this.computePrice('integral', this.integralPercent / 100);
    	/*{/literal}
    	{foreach from=$user_rank_list item=item}
    	set_price_note({$item.rank_id});
    	{/foreach}
    	{literal}*/
    },
    marketPriceSetted: function(){
    	this.computePrice('shop_price', 1/this.marketPriceRate, 'market_price');
    	this.computePrice('integral', this.integralPercent / 100);
    	/*{/literal}
    	{foreach from=$user_rank_list item=item}
    	set_price_note({$item.rank_id});
    	{/foreach}
	    {literal}*/
    },
  	integral_market_price: function(){
  		document.getElementsByName('market_price').value = parseInt(document.getElementsByName('market_price').value);
  	},
		parseint_integral: function(){
			 document.getElementsByName('integral').value = parseInt(document.getElementsByName('integral').value);
		},
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.config = {};
		},
		update: function () {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			
			$.get(baseURL + "sys/config/info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.config = r.config;
            });
		},
		del: function () {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/config/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate: function () {
            if(vm.validator()){
				return ;
			}

			var url = vm.config.id == null ? "sys/config/save" : "sys/config/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.config),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'key': vm.q.key},
                page:page
            }).trigger("reloadGrid");
		},
		validator: function () {
			if(isBlank(vm.config.key)){
				alert("参数名不能为空");
				return true;
			}

      if(isBlank(vm.config.value)){
        alert("参数值不能为空");
        return true;
    	}
    },
    computePrice: function(inputName, rate, priceName){
      var shopPrice = priceName == undefined ? document.getElementsByName('shop_price').value : document.getElementsByName(priceName).value;
      shopPrice = Utils.trim(shopPrice) != '' ? parseFloat(shopPrice)* rate : 0;
      if(inputName == 'integral')
      {
          shopPrice = parseInt(shopPrice);
      }
      shopPrice += "";

      n = shopPrice.lastIndexOf(".");
      if (n > -1)
      {
          shopPrice = shopPrice.substr(0, n + 3);
      }

      if (document.getElementsByName(inputName) != undefined)
      {
          document.getElementsByName(inputName).value = shopPrice;
      }
      else
      {
          document.getElementById(inputName).value = shopPrice;
      }
  	},
	}
});


